﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DownloadCoreTest
{
    using System.IO;
    using System.Threading;

    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using DownloaderCore;

    [TestClass]
    public class DownloadQueueTests
    {
        [TestMethod]
        public void StartQueueTest1()
        {
            var queue = new DownloadTaskManager();
            queue.StartQueue();
            Assert.IsTrue(true);
        }

        [TestMethod]
        public void StopQueueTest()
        {
            var queue = new DownloadTaskManager();
            queue.StartQueue();
            queue.StopQueue();
            Thread.Sleep(5000);
            Assert.IsTrue(queue.DownloadThread.IsCanceled);
        }

        [TestMethod]
        public void ParallelTaskCapTest()
        {
            var queue = new DownloadTaskManager();
            queue.StartQueue();
            queue.AddDownloadTask(new DownloadTask() { Name = "1" });
            queue.AddDownloadTask(new DownloadTask() { Name = "2" });
            queue.AddDownloadTask(new DownloadTask() { Name = "3" });

            Thread.Sleep(20000);
            Assert.IsTrue(true);
        }

        [TestMethod]
        public void ParallelTaskCapTestChangeCap()
        {
            var queue = new DownloadTaskManager();
            queue.StartQueue();
            queue.AddDownloadTask(new DownloadTask() { Name = "1" });
            queue.AddDownloadTask(new DownloadTask() { Name = "2" });
            queue.AddDownloadTask(new DownloadTask() { Name = "3" });
            //Thread.Sleep(2000);
            queue.MaximumParallelTasks = 2;

            Thread.Sleep(20000);
            Assert.IsTrue(true);
        }

        [TestMethod]
        public void OrderingTest()
        {
            var queue = new DownloadTaskManager();
            queue.StartQueue();
            queue.AddDownloadTask(new DownloadTask() { Name = "1" });
            queue.AddDownloadTask(new DownloadTask() { Name = "2" });
            queue.AddDownloadTask(new DownloadTask() { Name = "3" });

            Assert.IsTrue(queue.DownloadTasks[0].Name == "1" && queue.DownloadTasks[2].Name == "3");
        }

        [TestMethod]
        public void OrderingTest2()
        {
            var queue = new DownloadTaskManager();
            queue.StartQueue();
            queue.AddDownloadTask(new DownloadTask() { Name = "1" });
            queue.AddDownloadTask(new DownloadTask() { Name = "2" });
            queue.AddDownloadTask(new DownloadTask() { Name = "3" });
            Thread.Sleep(7000);
            Assert.IsTrue(queue.DownloadTasks[0].Name == "2" && queue.DownloadTasks[1].Name == "3");
        }

        [TestMethod]
        public void MovingTest()
        {
            var queue = new DownloadTaskManager();
            queue.StartQueue();
            queue.AddDownloadTask(new DownloadTask() { Name = "1" });
            queue.AddDownloadTask(new DownloadTask() { Name = "2" });
            var task3 = new DownloadTask() { Name = "3" };
            queue.AddDownloadTask(task3);
            Thread.Sleep(2000);
            queue.MoveDownloadTask(task3, 1);
            Assert.IsTrue(queue.DownloadTasks[0].Name == "1" && queue.DownloadTasks[1].Name == "3");
            Thread.Sleep(20000);
        }

        [TestMethod]
        public void RemoveTest()
        {
            var queue = new DownloadTaskManager();
            queue.StartQueue();
            var task1 = new DownloadTask() { Name = "1" };
            queue.AddDownloadTask(task1);
            queue.AddDownloadTask(new DownloadTask() { Name = "2" });
            queue.AddDownloadTask(new DownloadTask() { Name = "3" });
            Thread.Sleep(2000);
            queue.RemoveDownloadTask(task1);
          
            Assert.IsTrue(queue.DownloadTasks[0].Name == "2" && queue.DownloadTasks[1].Name == "3");
        }

        [TestMethod]
        public void DownloadTest1()
        {
            var queue = new DownloadTaskManager();

            var task = new DownloadTask(new Uri("http://download.microsoft.com/download/8/D/D/8DD7BDBA-CEF7-4D8E-8C16-D9F69527F909/ENU/x64/SQLManagementStudio_x64_ENU.exe", UriKind.Absolute), "Test2.exe")
                { FileTools = new DesktopFileTools() };
            queue.StartQueue();
            queue.AddDownloadTask(task);
            Thread.Sleep(60000);
            Assert.IsTrue(queue.DownloadTasks[0].Name == "2" && queue.DownloadTasks[1].Name == "3");
        }
    }

    internal class DesktopFileTools : IPlatformFileToolkit
    {
        public Stream OpenFile(string path)
        {
            return File.Open(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
        }

        public void DeleteFile(string path)
        {
            File.Delete(path);
        }

        public bool FileExists(string path)
        {
            return File.Exists(path);
        }

        public long GetFileSize(string path)
        {
            return new FileInfo(path).Length;
        }
    }
}
